<template>
  <a-modal title="设置管理员" :visible="flag" @ok="handleOk" @cancel="()=>this.$emit('cancel')">
    <a-form :form="form">
      <a-row>
        <a-col>
          <a-form-item label="权限" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-select v-decorator="['user_role_id', {rules:[{required: true, message: '请选择权限'}]}]">
              <a-select-option
                v-for="(item,index) in role"
                :key="index"
                :value="item.id"
              >{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col>
          <a-form-item label="账号名称" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-input v-decorator="['username', {rules:[{required: true, message: '请输入账号名称'}]}]" />
          </a-form-item>
          <a-form-item label="账户密码" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-input
              type="password"
              v-decorator="['password', {rules:[{required: true, message: '请输入密码'}]}]"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
export default {
  props: {
    flag: {
      type: Boolean,
      required: true,
      default: false
    },
    info: {
      type: Object,
      required: true
    },
    role: {
      type: Array,
      required: true
    }
  },
  data: function() {
    return {
      form: this.$form.createForm(this),
      editinfo: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
  },
  created() {},
  watch: {
    info(v) {
      if (v.id) {
        setTimeout(() => {
          this.form.setFieldsValue(pick(v, ['user_role_id', 'username', 'password']))
        }, 0)
      } else {
        this.form.resetFields()
      }
    }
  },
  methods: {
    filter(inputValue, path) {
      return path.componentOptions.children[0].text.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          const data = Object.assign(this.info, values)
          this.$post('/api/user/user_save', { data: JSON.stringify(data) }).then(res => {
            if (res.data.status === 1) {
              this.$message.success('保存账号成功')
              this.$emit('cancle')
              this.$emit('ok')
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>